package com.weifangfirecontrolformal.weifangfirecontrolformal.common.interceptor;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.annotaion.At;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.filter.RequestWrapper;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.model.RespEntity;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.utils.entity.Params;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * 参数校验拦截器
 * 
 * @author
 *
 */
public class ParamsInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		Params params = null;
		params = new Params(request);
		if (params == null || params.size() == 0) {
			if (request instanceof MultipartHttpServletRequest) {
			} else {
				RequestWrapper myRequestWrapper = new RequestWrapper(request);
				String body = myRequestWrapper.getBody();
				if (!"".equals(body)) {
					params = new Params(JSONObject.parseObject(body, Feature.OrderedField));
				}
			}
		}
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		HandlerMethod handlerMethod = null;
		if (handler instanceof HandlerMethod) {
			handlerMethod = (HandlerMethod) handler;
		} else {
			return true;
		}
		Method method = handlerMethod.getMethod();
		Object controller = handlerMethod.getBean();

		At at = method.getAnnotation(At.class);
		if (at != null) {
			// 开始做参数校验
			String result = "";
			for (String param : at.param()) {
				if (params.get(param)==null) {
					result += param + ",";
				}
			}
			if (!StrUtil.isBlank(result)) {
				result = result.substring(0, result.length() - 1);
				result = "[" + result + "]";
				response.getWriter().println(RespEntity.fail("缺少必要参数 " + result));
				return false;
			}
			// 开始做参数长度校验
//			Len[] lens = at.len();
//			Map<String, Object> lenMap = new HashMap<String, Object>(lens.length);
//			for (Len len : lens) {
//				lenMap.put(len.name(), len.value());
//			}
//			Dict lenDict = Dict.create(lenMap);
//			Iterator<Entry<String, Object>> entries = params.entrySet().iterator();
//			while (entries.hasNext()) {
//				@SuppressWarnings("rawtypes")
//				Entry entry = (Entry) entries.next();
//				String name = Convert.toStr(entry.getKey());
//				String value = Convert.toStr(entry.getValue());
//				int length = lenDict.getInt(name);
//				length = length == 0 ? lenDict.getInt("_all_") : length;
//				if (length <= 0) {
//					continue;
//				}
//				if (value.length() > length) {
//					result += StrUtil.format("{}长度不能超过{},", name, length);
//				}
//			}
//
//			if (!StrUtil.isBlank(result)) {
//				result = result.substring(0, result.length() - 1);
//				result = "[" + result + "]";
//				response.getWriter().println(RespEntity.fail("参数长度不合法  " + result));
//				return false;
//			}

		}
		
		return true;
	}

}
